package com.coder.rental.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.rental.entity.CarBrand;
import com.coder.rental.entity.CarMaker;
import com.coder.rental.service.ICarBrandService;
import com.coder.rental.service.ICarMakerService;
import com.coder.rental.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 汽车品牌 前端控制器
 * </p>
 *
 * @author zhaojj
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/rental/carBrand")
public class CarBrandController {
    @Resource
    private ICarBrandService carBrandService;

    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable("start") Integer start,
                         @PathVariable("size") Integer size,
                         @RequestBody CarBrand carBrand) {
        Page<CarBrand> page = new Page<>(start, size);
        return Result.success().setData(carBrandService.searchByPage(page, carBrand));
    }
    @PostMapping
    public Result save(@RequestBody CarBrand carBrand) {
        return carBrandService.save(carBrand)? Result.success() : Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody CarBrand carBrand) {
        return carBrandService.updateById(carBrand)? Result.success() : Result.fail();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids){
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        return carBrandService.removeByIds(list)? Result.success() : Result.fail();
    }
    // 根据厂商id获取所有对应的车辆品牌
    @GetMapping("/{makerId}")
    public Result selectByMakerId(@PathVariable Integer makerId){
        return Result.success().setData(carBrandService.selectByMakerId(makerId));
    }

    // 判断该品牌下是否有车辆
    @GetMapping("/hasCar/{brandId}")
    public Result hasCar(@PathVariable Integer brandId){
        return carBrandService.hasCar(brandId)? Result.success().setMessage("有") : Result.success().setMessage("无");
    }

    @GetMapping("exportExcel")
    public void export(HttpServletResponse response) throws IOException {
        //List<CarBrand> list = carBrandService.list();没有厂商名称
        List<CarBrand> list = carBrandService.selectAll();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("brandName","品牌名称");
        writer.addHeaderAlias("makerName","厂商名称");
        writer.addHeaderAlias("deleted","是否删除");
        writer.write(list,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
        String fileName = URLEncoder.encode("汽车品牌", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        writer.flush(response.getOutputStream(), true);
        writer.close();
    }
}
